package com.mew.chess.core.game.shbz;


import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.game.ShbzRoomManager;
import com.mew.chess.core.game.shbz.game.ShbzTableManager;
import com.mew.chess.core.game.shbz.game.common.FishTableUtil;
import com.mew.chess.core.game.shbz.game.common.FishUserBO;
import com.mew.chess.core.game.shbz.game.message.FishCMDMapType;
import com.mew.chess.core.game.shbz.game.message.request.SwitchBetRequestMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@ChessPlayAnnotation(ChessHandlerMapCmdType.ShbzSwitchBet)
@Service
@Slf4j
public class ShbzSwitchBetHandler implements ChessPlayHandler<SwitchBetRequestMessage> {

    @Override
    public void handle(ZebraCommand<SwitchBetRequestMessage> command, GameSession gameSession) {
        SwitchBetRequestMessage message = command.getRequest();


        ShbzRoomManager roomManager = ShbzGameManager.getInstance().getController(gameSession.getRoomId());
        if (message.getBetType()<1||message.getBetType()>roomManager.getBets().length){
            command.sendFailureCode(ResponseCode.BetError.code());
            return;
        }


        ShbzTableManager table = roomManager.getTableByUserId(gameSession.getUserId());
        FishUserBO userBO = table.getOnlineUserMap().get(gameSession.getUserId());
        userBO.setBetNo(message.getBetType());
        FishTableUtil.sendMsgByTable(table,message, FishCMDMapType.ShbzSwitchBet.code(),0);
    }
}
